

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="python" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="python" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>QUANTAXIS.QAEngine.QAThreadEngine &mdash; QUANTAXIS 1.0.39 documentation</title>
  

  
  
  
  

  

  
  
    

  

  
    <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../index.html" class="icon icon-home"> QUANTAXIS
          

          
          </a>

          
            
            
              <div class="version">
                1.0.39
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">QUANTAXIS</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>QUANTAXIS.QAEngine.QAThreadEngine</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for QUANTAXIS.QAEngine.QAThreadEngine</h1><div class="highlight"><pre>
<span></span><span class="c1"># coding:utf-8</span>

<span class="c1"># The MIT License (MIT)</span>
<span class="c1">#</span>
<span class="c1"># Copyright (c) 2016-2018 yutiansut/QUANTAXIS</span>
<span class="c1">#</span>
<span class="c1"># Permission is hereby granted, free of charge, to any person obtaining a copy</span>
<span class="c1"># of this software and associated documentation files (the &quot;Software&quot;), to deal</span>
<span class="c1"># in the Software without restriction, including without limitation the rights</span>
<span class="c1"># to use, copy, modify, merge, publish, distribute, sublicense, and/or sell</span>
<span class="c1"># copies of the Software, and to permit persons to whom the Software is</span>
<span class="c1"># furnished to do so, subject to the following conditions:</span>
<span class="c1">#</span>
<span class="c1"># The above copyright notice and this permission notice shall be included in all</span>
<span class="c1"># copies or substantial portions of the Software.</span>
<span class="c1">#</span>
<span class="c1"># THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR</span>
<span class="c1"># IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</span>
<span class="c1"># FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE</span>
<span class="c1"># AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</span>
<span class="c1"># LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,</span>
<span class="c1"># OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE</span>
<span class="c1"># SOFTWARE.</span>

<span class="kn">import</span> <span class="nn">datetime</span>
<span class="kn">import</span> <span class="nn">threading</span>
<span class="kn">import</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">queue</span> <span class="k">import</span> <span class="n">Queue</span>

<span class="kn">from</span> <span class="nn">QUANTAXIS.QAEngine.QATask</span> <span class="k">import</span> <span class="n">QA_Task</span>
<span class="kn">from</span> <span class="nn">QUANTAXIS.QAUtil</span> <span class="k">import</span> <span class="n">QA_util_log_info</span><span class="p">,</span> <span class="n">QA_util_random_with_topic</span>


<span class="sd">&quot;&quot;&quot;标准化的QUANATAXIS事件分发,可以快速引入和复用</span>
<span class="sd">每个事件需要携带一个方法,并且是需要立即被执行的时间才能使用这个事件方法&quot;&quot;&quot;</span>


<div class="viewcode-block" id="QA_Thread"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Thread">[docs]</a><span class="k">class</span> <span class="nc">QA_Thread</span><span class="p">(</span><span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        &#39;这是一个随意新建线程的生产者消费者模型&#39;</span>
<span class="sd">        其实有个队列， 队列中保存的是 QA_Task 对象 ， callback 很重要，指定任务的时候可以绑定 函数执行</span>
<span class="sd">        QA_Engine 继承这个类。</span>
<span class="sd">    &#39;&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">queue</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">threading</span><span class="o">.</span><span class="n">Thread</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">queue</span> <span class="o">=</span> <span class="n">Queue</span><span class="p">()</span> <span class="k">if</span> <span class="n">queue</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">queue</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">thread_stop</span> <span class="o">=</span> <span class="kc">False</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__flag</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Event</span><span class="p">()</span>     <span class="c1"># 用于暂停线程的标识</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__flag</span><span class="o">.</span><span class="n">set</span><span class="p">()</span>       <span class="c1"># 设置为True</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__running</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Event</span><span class="p">()</span>      <span class="c1"># 用于停止线程的标识</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__running</span><span class="o">.</span><span class="n">set</span><span class="p">()</span>      <span class="c1"># 将running设置为True</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">QA_util_random_with_topic</span><span class="p">(</span><span class="n">topic</span><span class="o">=</span><span class="s1">&#39;QA_Thread&#39;</span><span class="p">,</span> <span class="n">lens</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span> <span class="k">if</span> <span class="n">name</span> <span class="ow">is</span> <span class="kc">None</span> <span class="k">else</span> <span class="n">name</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">idle</span> <span class="o">=</span> <span class="kc">False</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39;&lt;QA_Thread</span><span class="si">{}</span><span class="s1">  id=</span><span class="si">{}</span><span class="s1">&gt;&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="nb">id</span><span class="p">(</span><span class="bp">self</span><span class="p">))</span>

<div class="viewcode-block" id="QA_Thread.run"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Thread.run">[docs]</a>    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">while</span> <span class="bp">self</span><span class="o">.</span><span class="n">__running</span><span class="o">.</span><span class="n">isSet</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__flag</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
            <span class="k">while</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">thread_stop</span><span class="p">:</span>
                <span class="s1">&#39;这是一个阻塞的队列,避免出现消息的遗漏&#39;</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">queue</span><span class="o">.</span><span class="n">empty</span><span class="p">()</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">:</span>
                        <span class="n">_task</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">queue</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>  <span class="c1"># 接收消息</span>
                        <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">_task</span><span class="p">,</span> <span class="n">QA_Task</span><span class="p">)</span>
                        <span class="k">if</span> <span class="n">_task</span><span class="o">.</span><span class="n">worker</span> <span class="o">!=</span> <span class="kc">None</span><span class="p">:</span>

                            <span class="n">_task</span><span class="o">.</span><span class="n">do</span><span class="p">()</span>

                            <span class="bp">self</span><span class="o">.</span><span class="n">queue</span><span class="o">.</span><span class="n">task_done</span><span class="p">()</span>  <span class="c1"># 完成一个任务</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="k">pass</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">idle</span> <span class="o">=</span> <span class="kc">True</span>

                        <span class="c1"># Mac book下风扇狂转，如果sleep cpu 占用率回下降</span>
                        <span class="c1"># time.sleep(0.01)</span>
                <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">e</span></div>

<div class="viewcode-block" id="QA_Thread.pause"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Thread.pause">[docs]</a>    <span class="k">def</span> <span class="nf">pause</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__flag</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span></div>

<div class="viewcode-block" id="QA_Thread.resume"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Thread.resume">[docs]</a>    <span class="k">def</span> <span class="nf">resume</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__flag</span><span class="o">.</span><span class="n">set</span><span class="p">()</span>    <span class="c1"># 设置为True, 让线程停止阻塞</span></div>

<div class="viewcode-block" id="QA_Thread.stop"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Thread.stop">[docs]</a>    <span class="k">def</span> <span class="nf">stop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># self.__flag.set()       # 将线程从暂停状态恢复, 如何已经暂停的话</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__running</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">thread_stop</span> <span class="o">=</span> <span class="kc">True</span>        <span class="c1"># 设置为False</span></div>

    <span class="k">def</span> <span class="nf">__start</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">queue</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>

<div class="viewcode-block" id="QA_Thread.put"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Thread.put">[docs]</a>    <span class="k">def</span> <span class="nf">put</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">task</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">queue</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">task</span><span class="p">)</span></div>

<div class="viewcode-block" id="QA_Thread.put_nowait"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Thread.put_nowait">[docs]</a>    <span class="k">def</span> <span class="nf">put_nowait</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">task</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">queue</span><span class="o">.</span><span class="n">put_nowait</span><span class="p">(</span><span class="n">task</span><span class="p">)</span></div>

<div class="viewcode-block" id="QA_Thread.get"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Thread.get">[docs]</a>    <span class="k">def</span> <span class="nf">get</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">task</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">task</span><span class="p">)</span></div>

<div class="viewcode-block" id="QA_Thread.get_nowait"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Thread.get_nowait">[docs]</a>    <span class="k">def</span> <span class="nf">get_nowait</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">task</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">get_nowait</span><span class="p">(</span><span class="n">task</span><span class="p">)</span></div>

<div class="viewcode-block" id="QA_Thread.qsize"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Thread.qsize">[docs]</a>    <span class="k">def</span> <span class="nf">qsize</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">queue</span><span class="o">.</span><span class="n">qsize</span><span class="p">()</span></div></div>


<div class="viewcode-block" id="QA_Engine"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Engine">[docs]</a><span class="k">class</span> <span class="nc">QA_Engine</span><span class="p">(</span><span class="n">QA_Thread</span><span class="p">):</span>
    <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        QA_Thread的区别是，</span>
<span class="sd">        QA_Thread 只是单纯一个线程，里面有个队列执行 QA_Task 的do 方法</span>

<span class="sd">        QA_Engine 有 kernels_dict词典，可以指定 {名字：QA_Thread}，</span>
<span class="sd">        QA_Engine自己有一个线程可以 遍历kernels_dict中QA_Thread 中的多个线程去执行</span>

<span class="sd">        kernel 已更正(之前误写成kernal) @2018/05/28</span>
<span class="sd">    &#39;&#39;&#39;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">queue</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">queue</span><span class="o">=</span><span class="n">queue</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="s1">&#39;QA_Engine&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">kernels_dict</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__flag</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Event</span><span class="p">()</span>     <span class="c1"># 用于暂停线程的标识</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__flag</span><span class="o">.</span><span class="n">set</span><span class="p">()</span>       <span class="c1"># 设置为True</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__running</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Event</span><span class="p">()</span>      <span class="c1"># 用于停止线程的标识</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__running</span><span class="o">.</span><span class="n">set</span><span class="p">()</span>      <span class="c1"># 将running设置为True</span>

    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="s1">&#39; &lt;QA_ENGINE with </span><span class="si">{}</span><span class="s1"> kernels&gt;&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">kernels_dict</span><span class="o">.</span><span class="n">keys</span><span class="p">()))</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">kernel_num</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">kernels_dict</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>

<div class="viewcode-block" id="QA_Engine.create_kernel"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Engine.create_kernel">[docs]</a>    <span class="k">def</span> <span class="nf">create_kernel</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="c1"># ENGINE线程创建一个事件线程</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">kernels_dict</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">QA_Thread</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="n">name</span><span class="p">)</span></div>

<div class="viewcode-block" id="QA_Engine.register_kernel"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Engine.register_kernel">[docs]</a>    <span class="k">def</span> <span class="nf">register_kernel</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">kernel</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">name</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">kernels_dict</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">kernels_dict</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">kernel</span></div>

<div class="viewcode-block" id="QA_Engine.start_kernel"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Engine.start_kernel">[docs]</a>    <span class="k">def</span> <span class="nf">start_kernel</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">kernels_dict</span><span class="p">[</span><span class="n">name</span><span class="p">]</span><span class="o">.</span><span class="n">start</span><span class="p">()</span></div>

<div class="viewcode-block" id="QA_Engine.stop_kernel"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Engine.stop_kernel">[docs]</a>    <span class="k">def</span> <span class="nf">stop_kernel</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">kernels_dict</span><span class="p">[</span><span class="n">name</span><span class="p">]</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
        <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">kernels_dict</span><span class="p">[</span><span class="n">name</span><span class="p">]</span></div>

<div class="viewcode-block" id="QA_Engine.run_job"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Engine.run_job">[docs]</a>    <span class="k">def</span> <span class="nf">run_job</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">task</span><span class="p">):</span>
        <span class="sd">&#39;&#39;&#39;</span>
<span class="sd">        把QA_Task 放到 kerneals_dict 词典中的task.engine 名字， 对应的 QA_Thread中的队列中，</span>
<span class="sd">        :param task:  type str 字符串</span>
<span class="sd">        :return: None</span>
<span class="sd">        &#39;&#39;&#39;</span>

        <span class="c1"># 🛠todo 建议把 engine 变量名字 改成  engine_in_kerneals_dict_name, 便于理解</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">kernels_dict</span><span class="p">[</span><span class="n">task</span><span class="o">.</span><span class="n">engine</span><span class="p">]</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">task</span><span class="p">)</span></div>


<div class="viewcode-block" id="QA_Engine.stop_all"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Engine.stop_all">[docs]</a>    <span class="k">def</span> <span class="nf">stop_all</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">kernels_dict</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
            <span class="n">item</span><span class="o">.</span><span class="n">stop</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">kernels_dict</span> <span class="o">=</span> <span class="p">{}</span></div>

<div class="viewcode-block" id="QA_Engine.stop"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Engine.stop">[docs]</a>    <span class="k">def</span> <span class="nf">stop</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># self.__flag.set()       # 将线程从暂停状态恢复, 如何已经暂停的话</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__running</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">thread_stop</span> <span class="o">=</span> <span class="kc">True</span></div>

<div class="viewcode-block" id="QA_Engine.pause"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Engine.pause">[docs]</a>    <span class="k">def</span> <span class="nf">pause</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__flag</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span></div>

<div class="viewcode-block" id="QA_Engine.resume"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Engine.resume">[docs]</a>    <span class="k">def</span> <span class="nf">resume</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__flag</span><span class="o">.</span><span class="n">set</span><span class="p">()</span>    <span class="c1"># 设置为True, 让线程停止阻塞</span></div>

<div class="viewcode-block" id="QA_Engine.run"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Engine.run">[docs]</a>    <span class="k">def</span> <span class="nf">run</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">while</span> <span class="bp">self</span><span class="o">.</span><span class="n">__running</span><span class="o">.</span><span class="n">isSet</span><span class="p">():</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">__flag</span><span class="o">.</span><span class="n">wait</span><span class="p">()</span>
            <span class="k">while</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">thread_stop</span><span class="p">:</span>
                <span class="s1">&#39;这是一个阻塞的队列,避免出现消息的遗漏&#39;</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">queue</span><span class="o">.</span><span class="n">empty</span><span class="p">()</span> <span class="ow">is</span> <span class="kc">False</span><span class="p">:</span>
                        <span class="n">_task</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">queue</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>  <span class="c1"># 接收消息</span>
                        <span class="c1">#print(&quot;queue left %d&quot;%self.queue.qsize())</span>
                        <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">_task</span><span class="p">,</span> <span class="n">QA_Task</span><span class="p">)</span>
                        <span class="c1">#print(_task)</span>


                        <span class="c1"># 🛠todo 建议把 engine 变量名字 改成  engine_in_kernels_dict_name, 便于理解</span>
                        <span class="k">if</span> <span class="n">_task</span><span class="o">.</span><span class="n">engine</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>  <span class="c1"># _task.engine 是字符串，对于的是 kernels_dict 中的 线程对象</span>

                            <span class="c1"># 如果不指定线程 就在ENGINE线程中运行</span>
                            <span class="n">_task</span><span class="o">.</span><span class="n">do</span><span class="p">()</span>
                            <span class="bp">self</span><span class="o">.</span><span class="n">queue</span><span class="o">.</span><span class="n">task_done</span><span class="p">()</span>
                        <span class="k">else</span><span class="p">:</span>
                            <span class="c1"># 把当前任务，用_task.engin名字对应的  kernels_dict 线程去执行</span>
                            <span class="bp">self</span><span class="o">.</span><span class="n">run_job</span><span class="p">(</span><span class="n">_task</span><span class="p">)</span>
                            <span class="bp">self</span><span class="o">.</span><span class="n">queue</span><span class="o">.</span><span class="n">task_done</span><span class="p">()</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">idle</span> <span class="o">=</span> <span class="kc">True</span>

                    <span class="c1">#Mac book下风扇狂转，如果sleep cpu 占用率回下降</span>
                    <span class="c1">#time.sleep(0.01)</span>

                <span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="n">e</span></div>
                    <span class="c1"># self.run()</span>

<div class="viewcode-block" id="QA_Engine.clear"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Engine.clear">[docs]</a>    <span class="k">def</span> <span class="nf">clear</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">res</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">kernels_dict</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">item</span><span class="o">.</span><span class="n">queue</span><span class="o">.</span><span class="n">empty</span><span class="p">():</span>
                <span class="n">res</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">item</span><span class="o">.</span><span class="n">idle</span><span class="p">:</span>
                <span class="n">res</span> <span class="o">=</span> <span class="kc">False</span>

            <span class="n">item</span><span class="o">.</span><span class="n">queue</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">queue</span><span class="o">.</span><span class="n">empty</span><span class="p">():</span>
            <span class="n">res</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="k">return</span> <span class="n">res</span></div>

<div class="viewcode-block" id="QA_Engine.join"><a class="viewcode-back" href="../../../source/QUANTAXIS.QAEngine.html#QUANTAXIS.QAEngine.QAThreadEngine.QA_Engine.join">[docs]</a>    <span class="k">def</span> <span class="nf">join</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">kernels_dict</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
            <span class="n">item</span><span class="o">.</span><span class="n">queue</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">queue</span><span class="o">.</span><span class="n">join</span><span class="p">()</span></div></div>


<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="kn">import</span> <span class="nn">queue</span>
    <span class="n">q</span> <span class="o">=</span> <span class="n">queue</span><span class="o">.</span><span class="n">Queue</span><span class="p">()</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2018, yutiansut.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'1.0.39',
            LANGUAGE:'python',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  
  
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>